package com.kaifamiao.thread.d0928;

import java.util.concurrent.atomic.AtomicInteger;

public class Demo05 {

    public static void main(String[] args) {
        MyRunnable run = new MyRunnable();

        for (int i = 0; i < 100; i++) {
            Thread t = new Thread(run);
            t.start();
        }

    }
}

class MyRunnable implements Runnable{

    private volatile AtomicInteger count = new AtomicInteger(0);

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            // 1.从共享数据中读取数据到本线程栈中.
            // 2.修改本线程栈中变量副本的值
            // 3. 会把本线程栈中变量副本的值赋值给共享数据.
//            synchronized (this){
//                count++;
//            }

            count.getAndIncrement(); // count ++
            System.out.println(count);
        }
    }

}
